<template>
  <div class="params">
    <!-- 面包屑 -->
    <div class="nav">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/layout/params' }"
          >规格参数</el-breadcrumb-item
        >
        <el-breadcrumb-item>规格包装</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 搜索框 -->
    <div class="header">
      <el-input v-model="inp" @change="searchInput"/>
      <el-button type="primary" >查询</el-button>

      <el-button type="primary">查看</el-button>
      <el-button type="primary" @click="showParams">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" class="mytable" border>
      <el-table-column prop="itemCatId" label="规格参数ID" width="120">
      </el-table-column>
      <el-table-column prop="id" label="类目ID" width="120"> </el-table-column>
      <el-table-column prop="specsName" label="规格名称" width="120">
      </el-table-column>
      <el-table-column prop="paramData" label="规格参数" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作" width="280">
        <template>
          <el-button size="mini" icon="el-icon-search">查看</el-button>
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Mypagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @changePage="changePage"
    />
    <!-- 弹框 -->
    <ParamsDialog ref="dialog"/>
  </div>
</template>

<script>
import Mypagination from "@/components/Mypagination.vue";
import ParamsDialog from "./paramsDialog.vue";
export default {
  data() {
    return {
      inp: "",
      tableData: [],
      total: 1,
      pageSize: 1,
      currentPage: 1,
      list: [],
      type: 1,
    };
  },
  methods: {
    showParams(){
      this.$refs.dialog.dialogVisible = true
    },
    searchInput(val) {
      // 没有数据页码都是1
      if (!val) {
        this.http(1);
        return;
      }
      console.log(val);
      this.$api
        .searchParams({
          search: val,
        })
        .then(res => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.currentPage = 1;
            this.list = res.data.result;
            this.total = res.data.result.length;
            this.pageSize = 3; // 每页显示的条数
            this.tableData = res.data.result.slice(0, 3);
            this.type = 2; //
          } else {
            this.tableData = [];
            // this.type=1
            this.total = 1;
            this.pageSize = 1;
            this.type = 1;
          }
        });
    },
    http(page) {
      this.$api
        .getParams({
          page,
        })
        .then(res => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.tableData = res.data.result;
            this.total = res.data.total;
            this.pageSize = res.data.pageSize;
          }
        });
    },
    changePage(page) {
      this.currentPage = page; // 改变当前页码
      this.http(page);
      if (this.type === 1) {
        this.http(page);
      } else {
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    // itemCatId: this.treeData.cid,
              // paramsContent: JSON.stringify(this.dynamicValidateForm.groups),
              // specsName: this.treeData.name,
  },
  mounted() {
    this.http(1);
  },

  components: { Mypagination, ParamsDialog },
};
</script>

<style lang="scss" scoped>
.params {
  margin: 10px;
}
.nav {
  padding: 10px;
}
.header {
  padding: 10px;
  display: flex;
  // justify-content: space-between;
  background: #fff;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
}
.mytable {
  margin: 10px auto;
}
</style>
